using System;
using System.Collections.Generic;
using System.Text;
using MacomberMap.Common.User_Interfaces.Components;
using System.Data.Common;
using System.Xml;
using MacomberMap.Common.Types;

namespace MacomberMap.Common.Components
{
    /// <summary>
    /// This class holds information on a unit
    /// </summary>
    [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.Both)]
    public class MM_Unit : MM_Element
    {
        #region Variable declarations
        /// <summary>A collection of unit types</summary>
        private static List<String> UnitTypes = new List<string>(20);

        /// <summary>The type of the unit</summary>
        private int _UnitType;

        /// <summary>Whether the unit is a resource controller</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public bool IsRC;

        /// <summary>The type of the unit</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public String UnitType
        {
            get
            {
                if (_UnitType >= UnitTypes.Count)
                    return "Unknown";
                else
                    return UnitTypes[_UnitType];
            }
            set
            {
                _UnitType = UnitTypes.IndexOf(value);
                if (_UnitType == -1)
                    lock (this)
                    {
                        _UnitType = UnitTypes.Count;
                        UnitTypes.Add(value);
                    }
            }
        }
        #endregion


        #region Initialization
        /// <summary>
        /// Initialize an empty unit
        /// </summary>
        public MM_Unit()
            : base()
        {
            this.ElemType = MM_Overall_Display.ElemTypes["Unit"];
        }


        /// <summary>
        /// Initialize a unit against a database reader
        /// </summary>
        /// <param name="ElementSource"></param>
        public MM_Unit(DbDataReader ElementSource)
            : base(ElementSource)
        {
            this.ElemType = MM_Overall_Display.ElemTypes["Unit"];
        }

        /// <summary>
        /// Initialize a unit against an XML document
        /// </summary>
        /// <param name="ElementSource"></param>
        public MM_Unit(XmlElement ElementSource)
            : base(ElementSource)
        {
            this.ElemType = MM_Overall_Display.ElemTypes["Unit"];
        }
        #endregion
    }
}
